<?php
/**
 * Created by PhpStorm.
 * User: dell
 * Date: 15-4-27
 * Time: 下午1:00
 */

class order extends adminBase{
    function __construct(){
        parent::__construct();
        $this->cms->setPageTitle("订单管理");
        $this->cms->setControlFile("admin/tpl/order/order_control.json");
        $admin_info=$this->user->getLoginInfo();
        $user=UserMode::init();
        $levels=$user->getLevels($admin_info['id']);
        if(!$user->checkLevels(9,$levels)){
            $r['message']="您不具有此权限！";
            View::displayAsHtml($r,"plugin/state/tips.php");
            exit();
        }
    }

    function indexTask($shopId,$state,$service_id,$real_name,$tel,$page=1){
        $this->cms->setActionTitle("普通商品订单");
        $user=UserMode::init();
        $userM=UserModule::init('user');
        $order=OrderMode::init();
        $service=ServiceMode::init();
        $shop=ShopMode::init();
        $r['shopId']=$shopId;
        $r['state']=$state;
        $r['service_id']=$service_id;
        if(isset($real_name)||isset($tel)){
            if(isset($real_name))
            $user_id_byname=$user->getUserIdByRealName($real_name);
            if(isset($tel))
            $user_id_bytel=$userM->getIdByUsername($tel);
            if(isset($user_id_byname)){
                $user_id=$user_id_byname;
            }else if(isset($user_id_bytel)){
                $user_id=$user_id_bytel;
            }else{
                $user_id=-1;
            }
        }else{
            $user_id=null;
        }
        $url=WebRouter::init()->getQuestion("page=");
        $page=$page>=1? $page: 1;
        $countOfPage=20;
        $offset=($page-1)*$countOfPage;

        list($r['order_info'],$order_total)=$order->getList($user_id,$shopId,$state,null,null,'normal',null,$service_id,$countOfPage,$offset);
        $r['order_info']=is_array($r['order_info'])?$r['order_info']:array();
        foreach($r['order_info'] as &$v){
            $service_info=$service->getDetail($v['service_id']);
            $v['service_id']=$service_info['service_name'];
            $userInUserInfo=$user->getDetail($v['user_id']);
            $userInUser=$user->getDetailInUser($v['user_id']);
            $v['user_id']=$userInUserInfo['real_name'];
            $v['shop_id']=$userInUser['username'];
            $buyerInfo=$user->getDetail($v['buyer_id']);
            $a=$user->getDetailInUser($v['buyer_id']);
            $v['express_name']=$user->getDetailInUser($v['buyer_id'])['username'];
            $v['buyer_id']=$buyerInfo['real_name'];
        }
        list($r['service_info'],$service_total)=$service->getList(2,99,$offset);
        list($r['shop_info'],$shop_total)=$shop->getList(2,20,$offset);
        $r['orderState']=array(
            'unpaid'=>"未付款",'unshipped'=>"未发货",'shipped'=>"已向服务站发货",'shipped_direct'=>"已向用户发货",
            'service'=>"等待服务站配送",'received'=>"用户已签收",'confirmed'=>"用户已确认",'refunding'=>"进入退货流程",
            'evaluate'=>"已评价",'assigned'=>"其他");
        $r['pages']=new Page($page,$order_total,$countOfPage);
        $r['pages']->setPageCallback(function($page)use($url,$shopId,$service_id,$state,$tel,$real_name,$tel){
            return $url.$page."&shopId=".$shopId."&service_id=".$service_id."&state=".$state."&real_name=".$real_name."&tel=".$tel;
        });
        $this->cms->tableScene($r,"admin/tpl/order/order.php");
    }

    function serviceTask($shopId,$state,$service_id,$real_name,$tel,$page=1){
        $this->cms->setActionTitle("服务商品订单");
        $user=UserMode::init();
        $userM=UserModule::init('user');
        $order=OrderMode::init();
        $service=ServiceMode::init();
        $shop=ShopMode::init();
        $r['shopId']=$shopId;
        $r['state']=$state;
        $r['service_id']=$service_id;
        if(isset($real_name)||isset($tel)){
            if(isset($real_name))
                $user_id_byname=$user->getUserIdByRealName($real_name);
            if(isset($tel))
                $user_id_bytel=$userM->getIdByUsername($tel);
            if(isset($user_id_byname)){
                $user_id=$user_id_byname;
            }else if(isset($user_id_bytel)){
                $user_id=$user_id_bytel;
            }else{
                $user_id=-1;
            }
        }else{
            $user_id=null;
        }
        $url=WebRouter::init()->getQuestion("page=");
        $page=$page>=1? $page: 1;
        $countOfPage=20;
        $offset=($page-1)*$countOfPage;

        list($r['order_info'],$order_total)=$order->getList($user_id,$shopId,$state,null,null,'service',null,null,$countOfPage,$offset);
        $r['order_info']=is_array($r['order_info'])?$r['order_info']:array();
        foreach($r['order_info'] as &$v){
            $service_info=$service->getDetail($v['service_id']);
            $v['service_id']=$service_info['service_name'];
            $userInUserInfo=$user->getDetail($v['user_id']);
            $userInUser=$user->getDetailInUser($v['user_id']);
            $v['user_id']=$userInUserInfo['real_name'];
            $v['shop_id']=$userInUser['username'];
        }
        list($r['service_info'],$service_total)=$service->getList('all',20,$offset);
        list($r['shop_info'],$shop_total)=$shop->getList('notAll',20,$offset);
        $r['orderState']=array(
            'unpaid'=>"未付款",'evaluate'=>"已评价",'unused'=>"未使用",'used'=>"已使用");
        $r['pages']=new Page($page,$order_total,$countOfPage);
        $r['pages']->setPageCallback(function($page)use($url,$shopId,$service_id,$state){
            return $url.$page."&shopId=".$shopId."&service_id=".$service_id."&state=".$state;
        });
        $this->cms->tableScene($r,"admin/tpl/order/order_service.php");
    }

    function detailTask($id){
        $this->cms->setActionTitle("订单基本信息");
        $order=OrderMode::init();
        $order_info=$order->getDetail($id);
        $ad=AddressMode::init();
        $userAddId=$order_info['order']['user_address_id'];
        if(!empty($userAddId)){
            $r['userAddress']=$ad->getUserAddressAsString($userAddId);
        }
        $user=UserMode::init();
        $r['user_real_name']=$user->getDetail($order_info['order']['user_id'])['real_name'];
        $r['user_phone']=UserModule::init('user')->getDetail($order_info['order']['user_id'])['username'];
        $r['order']=$order_info['order'];
        $r['goods_in_order']=$order->getGoodsInOrder($r['order']['id']);
        $r['goods_in_order']=is_array($r['goods_in_order'])?$r['goods_in_order']:array();
        $r['buyer_name']=$user->getDetail($r['order']['buyer_id'])['real_name'];
        $r['buyer_phone']=UserModule::init('user')->getDetail($r['order']['buyer_id'])['username'];
        $r['shop']=$order_info['shop'];
        $service=ServiceMode::init();
        $service_id=$r['order']['service_id'];
        $r['service_info']=$service->getDetail($service_id);
        $r['current_type']=$r['order']['type'];
        $r['shopType']=array("normal"=>"普通商品","commodity"=>"大宗商品","service"=>"服务商品","non_sale"=>"广告商品");
        $r['current_state']=$r['order']['state'];
        $r['orderState']=array(
        'unpaid'=>"未付款",'unshipped'=>"未发货",'shipped'=>"已发货",'shipped_direct'=>"已向用户发货",
            'service'=>"等待服务站配送",'received'=>"用户已签收",'confirmed'=>"用户已确认",'refunding'=>"进入退货流程",
            'evaluate'=>"已评价",'assigned'=>"其他");
        $this->cms->tableScene($r,"admin/tpl/order/orderDetail.php");
    }

    function recordTask($page=1){
        $this->cms->setActionTitle("大宗商品报备信息");
        $goods=GoodsMode::init();
        $shop=ShopMode::init();
        $record=CommodityMode::init();
        $url=WebRouter::init()->getQuestion("page=");
        $page=$page>=1? $page: 1;
        $countOfPage=20;
        $offset=($page-1)*$countOfPage;
        list($r['record'],$record_total)=$record->getList(null,$countOfPage,$offset,null);
        $r['record']=is_array($r['record'])?$r['record']:array();
        foreach($r['record'] as &$v){
            $r['goods_info']=$goods->getSimpleInfo($v['gopen_id']);
            $r['shop_info']=$shop->getDetail($r['goods_info']['shop_id']);
            $v['gopen_id']=$r['shop_info']['company'];
            $v['user_id']=$r['goods_info']['name'];
            $v['create_time']=date('Y-m-d',strtotime($v['create_time'] . '+15 day'));
        }
        $r['recordState']=array(
            'record'=>"报备",'group'=>"团购"
        );
        $r['buyState']=array(
            '1'=>"已购买",'0'=>"未购买"
        );
        $r['currentState']=array(
            'waiting'=>"等待客服确认",'saw'=>"已看房",'deposit'=>"已缴纳订金",'front'=>"已交首付",'all'=>"已交全款",'finish'=>"完成销售",'overtime'=>"报备超时",'fake'=>"虚假报备",'profit'=>"已收到分成"
        );
        $r['pages']=new Page($page,$record_total,$countOfPage);
        $r['pages']->setPageCallback(function($page)use($url){
            return $url.$page;
        });
        $this->cms->tableScene($r,"admin/tpl/order/order_record.php");
    }

    function recordDeleteTask($id){
        $record=CommodityMode::init();
        if($record->delete($id)){
            $r['message']="删除成功";
            $r['url']=WebRouter::init()->getAction("record");
        }else{
            $r['message']="删除失败，请重试！";
        }
        View::displayAsHtml($r,"plugin/state/tips.php");
    }
} 